﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Srvtools;

public partial class WorkTime_ViewDepartmentWorkOn : System.Web.UI.Page
{
    private WebDataSet wsViewDepWorkOnCheck;



    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            int year = DateTime.Now.Year;
            int Month = DateTime.Now.Month;

            DlYear.SelectedValue = year.ToString();
            DLMonth.SelectedValue = Month.ToString();


            MasterPage_MasterPage mpg = (MasterPage_MasterPage)this.Page.Master;
            mpg.HelpHref = "~/Help/WorkTime/查阅部门人员考勤.htm";
            mpg.PageName = "查看部门员工的考勤数据";
            mpg.PicUrl = "";
            mpg.IntiBar();
        }
    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WorkTime_ViewDepartmentWorkOn));
        this.wsViewDepWorkOnCheck = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.wsViewDepWorkOnCheck)).BeginInit();
        // 
        // WMaster
        // 
        this.wsViewDepWorkOnCheck.Active = true;
        this.wsViewDepWorkOnCheck.AlwaysClose = false;
        this.wsViewDepWorkOnCheck.PacketRecords = 0;
        this.wsViewDepWorkOnCheck.Position = -1;
        this.wsViewDepWorkOnCheck.RemoteName = "Common.InfoInti";
        this.wsViewDepWorkOnCheck.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.wsViewDepWorkOnCheck)).EndInit();
    }

    protected void Refresh_Click(object sender, EventArgs e)
    {
        string SelDepId = DepartmentDList1.SelectDepmanetId;
        if (SelDepId.Length > 0 && SelDepId != "0")
        {
            int year = int.Parse(DlYear.SelectedValue);
            int month = int.Parse(DLMonth.SelectedValue);
            GetEmps(SelDepId, 1,ddlStatus.SelectedValue);
            apg.CurrentPageIndex = 1;

        }
        else
        {
            string message = "请首先选择部门!";
            //this.Page.Response.Write("<script>alert(\"" + message + "\");</script>");
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"" + message + "\");", true);
        }
    }

    private void GetEmps(string SelDepId, int page, string Status)
    {
        InitializeComponent();

        //get the WorkOn data
        int count = 0;
        DataSet EmpList;
        string sSql;
        //行数
        sSql = "select count(Employees.EmployeeID) FROM  Employees INNER JOIN Department ON Employees.DepartmentId = Department.DepartmentId" +
            " WHERE (Employees.DepartmentId LIKE '" + SelDepId + "%') " + (Status != "-1" ? " And EmpStatus=" + Status : "");
        EmpList = wsViewDepWorkOnCheck.Execute(sSql);
        count = Convert.ToInt32(EmpList.Tables[0].Rows[0][0]);
        apg.RecordCount = count;
        apg.PageSize = 5;


        //分页
        int index = 0;
        if (count == 0)
        {
            return;
        }
        if (page >= 1)
        {
            index = count - (page - 1) * 5;
            if (index <= 0)
            {
                index = count % 5;
            }
        }
        sSql = "select top 5 Employees.EmployeeID, Employees.EmpName, Department.DepartmentName from  Employees INNER JOIN Department ON Employees.DepartmentId = Department.DepartmentId " +
                            "where (EmployeeID>=(select min(EmployeeID) from " +
                            "(select top " + index + " EmployeeID from Employees INNER JOIN Department ON Employees.DepartmentId = Department.DepartmentId " +
                            "where (Employees.DepartmentId LIKE '" + SelDepId + "%') " + (Status != "-1" ? " And EmpStatus=" + Status : "") +
                            " order by Employees.EmployeeID desc) as t)) AND (Employees.DepartmentId LIKE '" + SelDepId + "%') " + (Status != "-1" ? " And EmpStatus=" + Status : "") +
                            " order by Employees.EmployeeID asc";
        EmpList = wsViewDepWorkOnCheck.Execute(sSql);

        Rpt.DataSource = EmpList;
        Rpt.DataBind();

    }

    protected void Rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            Calendar calr = (Calendar)e.Item.FindControl("Calr");

            WorkTimeN_WorkONCalendar wcl = (WorkTimeN_WorkONCalendar)e.Item.FindControl("WorkONCalendar1");
            DateTime st = Convert.ToDateTime(DlYear.SelectedValue + "-" + DLMonth.SelectedValue + "-1");
            DateTime et = Convert.ToDateTime(DlYear.SelectedValue + "-" + DLMonth.SelectedValue + "-1").AddMonths(1).AddDays(-1);
            wcl.EmpId = drv["EmployeeId"].ToString();
            wcl.st = st.ToString("yyyy-MM-dd");
            wcl.et = et.ToString("yyyy-MM-dd");
            wcl.IsSelectDateTime = false;
        }
    }

    protected void apg_PageChanged(object sender, EventArgs e)
    {
        string SelDepId = DepartmentDList1.SelectDepmanetId;
        if (SelDepId.Length > 0 && SelDepId != "0")
        {
            int year = int.Parse(DlYear.SelectedValue);
            int month = int.Parse(DLMonth.SelectedValue);
            GetEmps(SelDepId, apg.CurrentPageIndex, ddlStatus.SelectedValue);
        }
        else
        {
            string message = "请首先选择部门!";
            //this.Page.Response.Write("<script>alert(\"" + message + "\");</script>");
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"" + message + "\");", true);
        }
    }
}
